package com.douqu.game.core.entity.ext.data.boon;

import com.bean.core.buffer.ByteBuffer;
import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.RechargeConfig;
import com.douqu.game.core.e.E_QueryRechargeCountType;
import com.douqu.game.core.e.E_RechargeGoodsType;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.util.CoreUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author wangzhenfei
 *         2017-10-28 16:06
 *         vip福利的缓存
 *
 */
public class RechargeRecordData extends BaseData {
    public static int RECHARGE_DAILY_GIFT_COUNT;

    /**<充值id，结束时间 map不为空为首冲完成>**/
    private Map<Integer, MouthCardRecord> rechargeRecordMap;

    /**vip特权礼包购买记录 存入vip等级**/
    private List<Integer> vipGiftBagRecordList;

    /**** 每日礼包购买记录 **/
    private List<Integer> dailyGiftRecordList;
    /**** 上次购买时间 **/
    private long lastBuyDailyTime;

    /**
     * 缓存从认证服务器查询的总充值数量(rmb)
     */
    private Map<Integer, Integer> rechargeTotalCountMap = new ConcurrentHashMap<>();

    private Player player;
    public RechargeRecordData(Player player) {
        this.player = player;
        rechargeRecordMap = new ConcurrentHashMap<>();
        vipGiftBagRecordList = new CopyOnWriteArrayList<>();
        dailyGiftRecordList = new CopyOnWriteArrayList<>();
        clearRechargeCount();
    }

    @Override
    public void init() {
    }

    @Override
    public void checkInit() {

    }

    @Override
    public void reset() {
        if(!CoreUtils.isTodayByOffset(lastBuyDailyTime, DataFactory.currentTime, ConstantFactory.DEFAULT_RESET_FRESH_TIMES_HOUR)){
            dailyGiftRecordList.clear();
        }
    }

    @Override
    public void checkReset() {
       reset();
    }

    public boolean hasRedPointRemind(){

        return false;
    }

    /**
     * 是否有每日礼包的红点
     * @return
     */
    public boolean hasDailyRewardGiftRedPoint(){
//        StableDataConfig config = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.DAILY_RECHARGE_GIFT.getCode());
//        if(player.getVipLevel() >= config.intValue){
            if(RECHARGE_DAILY_GIFT_COUNT == 0){
                List<RechargeConfig> configs = DataFactory.getInstance().getDataList(DataFactory.RECHARGE_KEY);
                for(RechargeConfig rechargeConfig : configs){
                    if(rechargeConfig.type == E_RechargeGoodsType.DAILY_GIFT.getCode()){
                        RECHARGE_DAILY_GIFT_COUNT ++;
                    }
                }
            }
            return dailyGiftRecordList.size() < RECHARGE_DAILY_GIFT_COUNT;
//        }
//        return false;
    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeShort(rechargeRecordMap.size());
        for(Map.Entry<Integer, MouthCardRecord> entry : rechargeRecordMap.entrySet())
        {
            buffer.writeInt(entry.getKey());
            entry.getValue().writeTo(buffer);
        }

        buffer.writeShort(vipGiftBagRecordList.size());
        for(Integer id : vipGiftBagRecordList){
            buffer.writeInt(id);
        }

        buffer.writeInt(1);

        //继续存新数据
        buffer.writeShort(dailyGiftRecordList.size());
        for(Integer integer : dailyGiftRecordList){
            buffer.writeInt(integer);
        }
        buffer.writeLong(lastBuyDailyTime);
        buffer.writeBoolean(false);

    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        int size = buffer.readShort();
        int key;
        MouthCardRecord cardRecord;
        for(int i = 0; i < size; i++){
            key = buffer.readInt();
            cardRecord = new MouthCardRecord();
            cardRecord.loadFrom(buffer);
            rechargeRecordMap.put(key, cardRecord);
        }
        size = buffer.readShort();
        for(int i = 0; i < size ; i++){
            vipGiftBagRecordList.add(buffer.readInt());
        }

        int temp = buffer.readInt();
        if(temp == 0)
           return;

        size = buffer.readShort();
        for(int i = 0; i < size; i++){
            dailyGiftRecordList.add(buffer.readInt());
        }
        lastBuyDailyTime = buffer.readLong();
        buffer.readBoolean();
    }

    public boolean hasRechargeRecord(int id){
        return rechargeRecordMap.get(id) != null;
    }

    public void addRecord(int id, long endTime){
        MouthCardRecord cardRecord = rechargeRecordMap.get(id);
        if(cardRecord == null){
            cardRecord = new MouthCardRecord();
        }
        cardRecord.setEndTime(endTime);
        rechargeRecordMap.put(id, cardRecord);
    }

    public Map<Integer, MouthCardRecord> getRechargeRecordMap() {
        return rechargeRecordMap;
    }

    public void  addGiftBagVipId(int id){
        if(!vipGiftBagRecordList.contains(id)){
            vipGiftBagRecordList.add(id);
        }
    }

    public boolean hasGiftBagForVipId(int id){
        return vipGiftBagRecordList.contains(id);
    }

    public List<Integer> getGiftBagRecordList() {
        return vipGiftBagRecordList;
    }

    /**
     * 今天是否领取了月卡奖励
     * @return
     */
    public boolean isMouthCardRewardToday(int rechargeId){
        return TimeUtils.isToday(rechargeRecordMap.get(rechargeId).getLastRewardTime());
    }

    /**
     * 记录当天月卡领取记录
     * @param rechargeId
     * @param currentTime
     */
    public void recordMouthCardRewardTime(int rechargeId, long currentTime){
        rechargeRecordMap.get(rechargeId).setLastRewardTime(currentTime);
    }


    public boolean isFirstRechargeComplete(){
        return rechargeRecordMap.size() > 0;
    }


    /**
     * 每日礼包是否购买
     * @param rechargeId
     * @return
     */
    public boolean isDailyGiftRecordHasBuy(int rechargeId){
        return dailyGiftRecordList.contains(rechargeId);
    }

    /**
     * 添加每日礼包购买记录
     * @param rechargeId
     */
    public void addDailyGiftRecord(int rechargeId){
        if(!isDailyGiftRecordHasBuy(rechargeId)){
            dailyGiftRecordList.add(rechargeId);
        }
        lastBuyDailyTime = DataFactory.currentTime;
    }

    public List<Integer> getDailyGiftRecordList() {
        return dailyGiftRecordList;
    }

    public void clearRechargeCount(){
        rechargeTotalCountMap.clear();
        rechargeTotalCountMap.put(E_QueryRechargeCountType.DAILY.getCode(), 0);
        rechargeTotalCountMap.put(E_QueryRechargeCountType.WEEKLY.getCode(), 0);
        rechargeTotalCountMap.put(E_QueryRechargeCountType.DEFINE.getCode(), 0);
    }

    public void addRechargeCount(int type, int count){
        if(count <= 0){
            return;
        }
        Integer temp = rechargeTotalCountMap.get(type);
        temp = temp == null ? count : count + temp;
        rechargeTotalCountMap.put(type, temp);
    }

    public int getRechargeCount(int type){
        Integer temp = rechargeTotalCountMap.get(type);
        temp = temp == null ? 0 : temp;
        return temp;
    }

    public void addRechargeCount(int diamond)
    {
        for(Integer type : rechargeTotalCountMap.keySet())
        {
            addRechargeCount(type, diamond);
        }
    }


    @Override
    public String toString() {
        return "RechargeRecordData{" +
                "rechargeRecordMap=" + rechargeRecordMap +
                ", giftBagRecordList=" + vipGiftBagRecordList +
                ", player=" + player +
                "} " + super.toString();
    }



    public class MouthCardRecord extends EntityObject{
        /**月卡结束时间**/
        private long endTime;
        /**上传领取月卡奖励的时间**/
        private long lastRewardTime;

        public void writeTo(ByteBuffer buffer) {
            buffer.writeLong(endTime);
            buffer.writeLong(lastRewardTime);
        }

        public void loadFrom(ByteBuffer buffer) {
            endTime = buffer.readLong();
            lastRewardTime = buffer.readLong();
        }


        public long getEndTime() {
            return endTime;
        }

        public void setEndTime(long endTime) {
            this.endTime = endTime;
        }

        public long getLastRewardTime() {
            return lastRewardTime;
        }

        public void setLastRewardTime(long lastRewardTime) {
            this.lastRewardTime = lastRewardTime;
        }
    }

}
